package com.eastfair.projectcore.controller;

import com.eastfair.cache.redis.RedisOps;
import com.eastfair.core.base.R;
import com.eastfair.core.encrypt.AesEncryptUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.NoSuchAlgorithmException;
import java.util.Map;


/**
 * <p>
 * 前端控制器
 * DB 雪花id的 WorkerID
 * </p>
 *
 * @author ligang
 * @date 2022-02-25
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/privateKey")
@Api(value = "privateKey", tags = "DB 雪花id的 WorkerID")
public class PrivateKeyController {

    @Autowired
    private RedisOps redisOps;

    /**
     * 创建公钥
     *
     * @return
     */
    @ApiOperation(value = "创建公钥", notes = "创建公钥")
    @GetMapping("/createRsa")
    public R<String> createRsa() {
        //生成公钥和私钥
        Map<Integer, String> keyMap = null;
        try {
            keyMap = AesEncryptUtil.genKeyPair();
            log.info("随机生成的公钥为:" + keyMap.get(0));
            log.info("随机生成的私钥为:" + keyMap.get(1));
            //一小时过期
            redisOps.setEx("privatekey:" + keyMap.get(0), keyMap.get(1), 3600);
            return R.success(keyMap.get(0));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return R.fail("失败");
    }


}
